package com.dinghui.ebookbag.module.middle.fragment;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.dinghui.ebookbag.BaseFragment;
import com.dinghui.ebookbag.R;
import com.dinghui.ebookbag.module.middle.CalendarActivity;
import com.dinghui.ebookbag.module.middle.ClassScheduleActivity;
import com.dinghui.ebookbag.module.middle.GreenInternetActivity;
import com.dinghui.ebookbag.module.middle.ScientificCalculatorActivity;
import com.dinghui.ebookbag.module.middle.StandardCalculatorActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class ToolHelperFragment extends BaseFragment {

    ;
    @BindView(R.id.class_schedule_ll)
    LinearLayout classScheduleLl; //课程表

    @BindView(R.id.calendar_ll)
    LinearLayout calendar; // 日历

    @BindView(R.id.calculator_ll)
    LinearLayout calculatorLl; // 计算器

    @BindView(R.id.green_internet_ll)
    LinearLayout greenInternetLl; //绿色上网

    @BindView(R.id.camera_ll)
    LinearLayout cameraLl;

    @BindView(R.id.photo_ll)
    LinearLayout photoLl;

    @BindView(R.id.download_assistant_ll)
    LinearLayout downloadAssistantLl; // 下载助手

    @BindView(R.id.update_ll)
    LinearLayout updateLl; // 更新



    private static final int REQUEST_CODE_ALBUM  = 100; //打开相册
    private static final int REQUEST_CODE_CAMERA = 101; //打开相机

    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tool_helpr, container, false);
    }

    @Override
    public void initView(View view) {

    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.class_schedule_ll, R.id.calendar_ll, R.id.calculator_ll, R.id.green_internet_ll,
              R.id.camera_ll, R.id.photo_ll, R.id.download_assistant_ll, R.id.update_ll})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.class_schedule_ll: // 课程表
                startActivity(new Intent(getActivity(), ClassScheduleActivity.class));
                break;
            case R.id.calendar_ll: // 日历
                startActivity(new Intent(getActivity(), CalendarActivity.class));
                break;
            case R.id.calculator_ll: // 计算器
                startActivity(new Intent(getActivity(), StandardCalculatorActivity.class));
                break;
            case R.id.green_internet_ll: // 绿色上网
                startActivity(new Intent(getActivity(), GreenInternetActivity.class));
                break;
            case R.id.camera_ll:
                requestCameraPermission();  //动态请求相机权限, 在其中若用户给予权限则请求相机拍照
                break;
            case R.id.photo_ll:
                requestAlbumPermission();
                //openAlbum(); // 调用相册
                break;
            case R.id.download_assistant_ll: // 下载助手

                break;
            case R.id.update_ll: // 更新

                break;
        }
    }

    //动态请求相机权限
    private void requestCameraPermission() {
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            // 请求权限
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
        } else {
            // 调用相机
            requestCamera();
        }
    }

    //动态请求相册权限
    private void requestAlbumPermission() {
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }else {
            openAlbum();
        }

    }

    // 调用相机
    private void requestCamera() {
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, REQUEST_CODE_CAMERA);
    }

    // 调用相册
    private void openAlbum() {
        Intent intent = new Intent();
        //intent.setAction(Intent.ACTION_GET_CONTENT); // ACTION_GET_CONTENT = "android.intent.action.GET_CONTENT"：用户选择特定类型的数据并返回，需要输入希望得到MIME类型
        intent.setAction(Intent.ACTION_VIEW); // ACTION_VIEW = "android.intent.action.VIEW"：将数据显示给用户
        intent.setType("image/*");
        startActivityForResult(intent, REQUEST_CODE_ALBUM);
    }

}
